package cn.yuan.user.service.impl;

import cn.yuan.basic.Exception.BusinessException;
import cn.yuan.basic.service.impl.BaseServiceImpl;
import cn.yuan.basic.util.MD5Utils;
import cn.yuan.basic.util.StrUtils;
import cn.yuan.basic.constant.UserConstants;
import cn.yuan.user.domain.Logininfo;
import cn.yuan.user.domain.User;
import cn.yuan.user.dto.UserDto;
import cn.yuan.user.mapper.LogininfoMapper;
import cn.yuan.user.mapper.UserMapper;
import cn.yuan.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void batchDel(Long[] ids) {

    }
    @Override
    public void save(User user) {
        super.save(user);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
    }

    @Override
    @Transactional
    public void phoneRegister(UserDto userDto){
        //1.校验数据
        String phone = userDto.getPhone();
        //1.1 参数不能为空
        if(StringUtils.isEmpty(phone)
                ||StringUtils.isEmpty(userDto.getVerifyCode())
                ||StringUtils.isEmpty(userDto.getPassword())
                ||StringUtils.isEmpty(userDto.getConfirmPwd()))
        {
            throw new BusinessException("请输入完整信息！！");
        }
        //1.2 手机不能被注册  查询user
        User userTmp = userMapper.loadByPhone(phone);
        if (userTmp!=null){
            throw new BusinessException("该账号已注册");
        }
        //1.3 两次密码是否一致
        if (!userDto.getPassword().equals(userDto.getConfirmPwd())){
            throw new BusinessException("密码不一致");
        }
        //2 验证码校验  不要区分大小写
        Object codeObj = redisTemplate.opsForValue().get(UserConstants.USER_REGISTER_CODE_PREFIX + phone);
        if(codeObj==null){
            throw new BusinessException("验证码失效，请重新获取");
        }
        String codeStr = (String) codeObj;
        String code = codeStr.split(":")[0];
        if(!code.equalsIgnoreCase(userDto.getVerifyCode())){// equalsIgnoreCase不区分大小写的equals方法
            throw new BusinessException("验证码输入错误！");
        }
        //3 注册
        //3.1 登录信息 和 用户
        User user = UserDto2User(userDto);
        Logininfo logininfo = user2LoginInfo(user);
        logininfoMapper.save(logininfo);
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }

    private User UserDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);//生成随机32位的salt
        String md5 = MD5Utils.encrypByMd5(userDto.getPassword() + salt);//加密加盐
        user.setSalt(salt);
        user.setPassword(md5);
        return user;
    }

    private Logininfo user2LoginInfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1);
        return logininfo;
    }

}
